new Vue({
  el: '#container',
  components: {
    'login': httpVueLoader('../login.vue')//加载需要使用的vue文件
  },      
  data: {
    portal_url: 'http://www.xuecheng.com',
    coursePubId: 0,
    articleTapList: ['课程介绍', '目录'],
    articleTapIndex: 0,
    course_index: {},
    myCourseRecord: {},
    starRank: '',
    companyOverview: {},
    courseComments: [],
    courseCommentCounts: 0,
    recentLearnedUsers: []
  },
  computed: {
    courseProgress: function() {
      let url = '#'
      let text = '马上学习'

      // 课程发布id
      let coursePubId = this.coursePubId
      // 课程是否已付费
      let paid = this.myCourseRecord.paid
      // 课程学习进度
      let teachplanId = this.myCourseRecord.teachplanId
      // 是否是付费课程
	  let marketJson = this.course_index.market;
	  
      let charge = this.course_index.charge

      // 免费课程
      if (charge === '201000') {
        // 未学习
        if (!teachplanId) {
          url = `/learing-course-video.html?coursePubId=${coursePubId}`
          text = '马上学习'
        }
        // 学习过
        else {
          url = `/learing-course-video.html?coursePubId=${coursePubId}&teachplanId=${teachplanId}`
          text = '继续学习'
        }
      }
      // 付费课程
      else if (charge === '201001') {
        // 学习记录不存在，未付费
        if (paid !== 1) {
          url = `/learing-pay.html?coursePubId=${coursePubId}`
          text = '马上购买'
        }
        // 学习记录存在，已付费
        else {
          // 未学习
          if (!teachplanId) {
            url = `/learing-course-video.html?coursePubId=${coursePubId}`
            text = '马上学习'
          }
          // 学习过
          else {
            url = `/learing-course-video.html?coursePubId=${coursePubId}&teachplanId=${teachplanId}`
            text = '继续学习'
          }
        }
      }
      return {
        url: url,
        text: text
      }
    },
    goToCommentUrl: function() {
      return `/learing-article-comment.html?coursePubId=${this.coursePubId}`
    }
  },
  mounted() {
	this.coursePubId =  location.pathname.replace(/[^0-9]/ig,"");
    // 获取课程发布信息，根据课程Id从索引库中查询数据
    search_course_index(this.coursePubId).then(data => {
      this.course_index = data
	  console.log(this.course_index)
      this.getCompanyOverview()
    })

	if(hasLogin()) {
		// 获取当前课程发布的学习进度
		my_course_record(this.coursePubId).then(data => {
		  // 如果没有学习进度记录，后台会返回null
		  if (data) {
			this.myCourseRecord = data
		  }
		})
	}

    // 课程评论统计(模拟)
    course_comment_statistics(this.coursePubId).then(data => {
      this.starRank = data.starRank
    })

    // 课程评论列表
    get_course_comment(
      1,
      4,
      JSON.stringify({
        targetId: this.coursePubId
      })
    ).then(data => {
      this.courseComments = data.items
      this.courseCommentCounts = data.counts
    })

    //获取当前课程发布的学习用户记录
    recent_learned_users(this.coursePubId).then(data => {
      this.recentLearnedUsers = data
    })
  },
  methods: {
	goPage() {
		if(hasLogin()){
			location.href=this.courseProgress.url
		}else {
			showLogin();
		}
	},
    getCompanyOverview() {
      company_overview(this.course_index.company_id).then(data => {
        this.companyOverview = data
      })
    },
    handleClickArticleTap(index) {
      this.articleTapIndex = index
    }
  }
})
